import { inject, onMounted } from "vue"
import { mapStore } from "@/stores/counter"
export const search = () => {
    const store = mapStore()
    const { DrawRect, DrawEvent } = L7.Draw
    let scene
    let map
    onMounted(() => {
        scene = inject('$scene')
        map = inject('$map')
    })

    const choice_search = () => {
        store.searchLists = []
        const drawRect = new DrawRect(scene, {});
        drawRect.enable();

        drawRect.on(DrawEvent.Change, (allFeatures) => {
            store.isSearch = true

            // drawRect.enabled = false
            drawRect.destroy()
            // drawRect.disable()

            // 获取坐标
            let position = allFeatures[0].geometry.coordinates
            // 转为turf
            let polygon = turf.polygon(position)
            // 点数据
            const features = scene.getLayers()[0].layerSource.data.dataArray
            features.forEach(feature => {
                const coordinates_lng = feature.coordinates[0]
                const coordinates_lat = feature.coordinates[1]
                feature.coordinates = [coordinates_lng, coordinates_lat]
                // 获取坐标
                let position_point = feature.coordinates
                // 转换为turf
                let point = turf.point(position_point)
                // 判断
                let result = turf.booleanContains(polygon, point)
                if (result) {
                    if (feature.rainy) {
                        store.info = '降雨量'
                        store.searchLists.push({
                            date: feature.date,
                            name: feature.name,
                            data: parseInt(feature.rainy),
                            center: [feature.coordinates[0], feature.coordinates[1]]
                        })
                    } else if (feature.sun) {
                        store.info = '光照强度'
                        store.searchLists.push({
                            date: feature.date,
                            name: feature.name,
                            data: parseInt(feature.sun),
                            center: [feature.coordinates[0], feature.coordinates[1]]
                        })
                    } else if (feature.temperature) {
                        store.info = '气温'
                        store.searchLists.push({
                            date: feature.date,
                            name: feature.name,
                            data: parseInt(feature.temperature),
                            center: [feature.coordinates[0], feature.coordinates[1]]
                        })
                    }
                }
            })
            console.log(store.searchLists)
        });


    }
    return {
        choice_search
    }
}